//
//  ZCNetworkingTools.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/24.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCNetworkingTools.h"


@implementation ZCNetworkingTools
//MARK: 单例创建对象
static id _instanceType;
+(instancetype)sharedNetWorkingTools
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instanceType = [[self alloc]init];
    });
    
    return _instanceType;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instanceType = [super allocWithZone:zone];
    });
    return _instanceType;
}
-(id)copyWithZone:(NSZone *)zone
{
    return _instanceType;
}


/**
 *  封装get/post请求
 *
 *  @param type     请求类型
 *  @param url      请求地址
 *  @param params   请求参数
 *  @param callBack 回调block
 */
-(void)requestWithRequestType:(RequestType)type url:(NSString*)url params:(NSDictionary*)params callBack:(callBack)callBack {
    
    if (type == GET) {
        
        [[AFHTTPSessionManager manager] GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            callBack(responseObject,nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            callBack(nil,error);
        }];
        
    }else{
        [[AFHTTPSessionManager manager] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            callBack(responseObject,nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            callBack(nil,error);
        }];
    }
}


/**
 *  获取“推荐关注”中左侧标签的列表
 *
 *  @param callBack 回调block
 */
-(void)requestAttentionList:(callBack)callBack {
    
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *params = @{@"a":@"category",@"c":@"subscribe"};
    
    [self requestWithRequestType:GET url:url params:params callBack:callBack];
}
/**
 *  获取“推荐关注”中左侧标签每个标签对应的推荐用户组
 *
 *  @param category_id 要查询的推荐标签的id
 *  @param page        页码数，默认为1
 *  @param callBack    回调block
 */
-(void)requestAttentionContentWithCategoryID:(NSInteger)category_id page:(NSInteger)page callBack:(callBack)callBack{
    
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *params = @{@"a":@"list",@"c":@"subscribe",@"category_id":[NSString stringWithFormat:@"%ld",category_id],@"page":@(page)};
    
    [self requestWithRequestType:GET url:url params:params callBack:callBack];
    
}
/**
 *  获取“标签订阅”页面中“推荐标签”中的内容
 *
 *  @param callBack 回调block
 */
-(void)requestRecommend:(callBack)callBack{
    
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *params = @{@"a":@"tag_recommend",@"action":@"sub",@"c":@"topic"};
    
    [self requestWithRequestType:GET url:url params:params callBack:callBack];
}

/**
 *  获取自定义类型的帖子
 *
 *  @param maxtime  最大帖子ID
 *  @param callBack 回调block
 */

-(void)requestEssenceWithMaxid:(NSString*)maxid  page:(NSInteger)page type:(NSInteger)type a:(NSString*)a callBack:(callBack)callBack{
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *paramas = [NSDictionary dictionary];
    
    if (maxid != nil) {
        
        paramas = @{@"a":a,@"c":@"data",@"maxtime":maxid,@"page":@(page),@"type":@(type)};
    }else{
        
        paramas = @{@"a":a,@"c":@"data",@"type":@(type)};
    }
    
    [self requestWithRequestType:GET url:url params:paramas callBack:callBack];
}

/**
 *  获取评论数据
 *
 *  @param dataID   帖子的id
 *  @param callBack 回调block
 */
-(void)requestCommentWithdataID:(NSString*)dataID lastcid:(NSString*)lastcid page:(NSInteger)page callBack:(callBack)callBack{
    
    NSDictionary *paramas;
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    if (lastcid == nil) {
        
        paramas = @{@"a":@"datalist",@"c":@"comment",@"data_id":dataID,@"hot":@"1"};
    }else{
        
        paramas = @{@"a":@"datalist",@"c":@"comment",@"data_id":dataID,@"hot":@"1",@"lastcid":lastcid,@"page":@(page)};
    }
    
    [self requestWithRequestType:GET url:url params:paramas callBack:callBack];
}


@end
